<template>
  <view class="change-email">
    <u-navbar
      :title="$t('user.Change-Number')"
      :autoBack="false"
			@leftClick="gotoBack"
      bgColor="#F6F6F6"
      titleStyle="font-weight: 500; color: #000000;"
      :placeholder="true"
    ></u-navbar>
    <view class="label">{{ $t("user.Phone-tips2") }}</view>
    <view class="content">
      <view class="input-box">
        <view class="prefix" @click="openAreaCodePopup">
          {{ AreaPhone }}
          <view class="triangle"> </view>
        </view>
        <input
          v-model="phoneNumber"
          class="email-view"
          :placeholder="$t('user.New-Number')"
        />
      </view>
      <view class="code-views">
        <input
          v-model="phoneNumberCode"
          :placeholder="$t('index.Verification-Code')"
        />
        <view v-if="!codeShow" class="code" @click="getCodeHandle">{{
          $t("index.GET")
        }}</view>
        <view v-else>
          <u-count-down
            :time="60 * 1000"
            format="ss"
            @change="onChange"
            @finish="onFinish"
          >
            <view class="count-down">
              <text class="count-down__item">{{ timeData.seconds }}</text
              >s
            </view>
          </u-count-down>
        </view>
      </view>
    </view>
    <button class="Change" @click="handleConfirm">
      {{ $t("index.Confirm") }}
    </button>

    <!-- 选择手机区号弹出框 -->
    <u-popup :show="showAreaCode">
      <view class="area-popup">
        <view class="area-popup__header">
          {{ $t("user.Select-Area-Code") }}
        </view>

        <view class="area-popup__input">
          <u-input
            v-model="searchValue"
            shape="circle"
            :placeholder="$t('feeServices.search')"
            prefixIcon="search"
            prefixIconStyle="font-size: 40rpx;color: #878787"
            placeholderStyle="{
							color: '#878787',
							fontSize:'26rpx'
						}"
          >
          </u-input>
          <view class="area-popup__input__btn" @click="closePopup">
            {{ $t("index.Cancel") }}
          </view>
        </view>

        <view class="area-popup__container">
          <scroll-view
            scroll-top="0"
            scroll-y="true"
            class="scroll-Y"
            :scroll-into-view="scrollIntoView"
          >
            <view v-for="(item, index) in filterAreasList" :key="index">
              <view
                :id="'s-' + filterAreasList[index].en.slice(0, 1)"
                class="scroll-view-letter"
                v-if="
                  index == 0 ||
                  filterAreasList[index].en.slice(0, 1) !==
                    filterAreasList[index - 1].en.slice(0, 1)
                "
                >{{ filterAreasList[index].en.slice(0, 1) }}</view
              >
              <view class="scroll-view-item" @click="selectAreaPhone(item)">
                <text>{{  locale == 'zh-Hans'? item.cn : item.en }}</text>
                <text>{{ item.code }}</text>
              </view>
            </view>
          </scroll-view>

          <!-- 右侧导航栏 -->
          <view class="suspension-frame" v-if="filterAreasList.length !== 0">
            <view
              class="text"
              v-for="(item, index) in filterAreasList"
              :key="index"
              v-if="
                index == 0 ||
                filterAreasList[index].en.slice(0, 1) !==
                  filterAreasList[index - 1].en.slice(0, 1)
              "
              :class="{ active: currentView == item.en.slice(0, 1) }"
              @click="onLocation(item)"
              >{{ filterAreasList[index].en.slice(0, 1) }}</view
            >
          </view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import areasList from "@/utils/GlobalPhonePrefix.js";
import { changeUserPhone } from "@/services/user";
import { sendSms } from "@/utils/index";
export default {
  data() {
    return {
      areasList,
      phoneNumber: "",
      phoneNumberCode: "",
      codeShow: false,
      timeData: {},
      showAreaCode: false,
      searchValue: "",
      AreaPhone: "+86",
      scrollIntoView: "",
      currentView: "",
			locale: ''
    };
  },
  onShow() {
    this.currentView = this.areasList[0]?.en?.slice(0, 1);
		this.locale = uni.getLocale()
  },
  computed: {
    filterAreasList() {
      if (
        this.searchValue == "" ||
        this.searchValue == undefined ||
        this.searchValue == null
      ) {
        let arr = this.areasList.filter((item) => {
          return item.en === "China" || item.en === "Russia";
        });
        this.currentView = arr.length > 0 ? arr[0].en.slice(0, 1) : "";
        return arr;
      } else {
        let arr = this.areasList.filter(
          (t) =>
            t.en.toLowerCase().indexOf(this.searchValue.toLowerCase()) !== -1 &&
            (t.en === "China" || t.en === "Russia")
        );
        this.currentView = arr.length > 0 ? arr[0].en.slice(0, 1) : "";
        return arr;
      }
    },
  },
  methods: {
		gotoBack() {
			uni.redirectTo({
				url: '/pagesUser/setting/PhoneNumber?phone='+ this.$store.state.userInfo.userPhone
			});
		},
    async handleConfirm() {
      if (!this.phoneNumber) {
        return uni.showToast({
          title: this.$t("user.phoneNumber"),
          icon: "error",
        });
      }
      if (!this.phoneNumberCode) {
        return uni.showToast({
          title: this.$t("user.phoneNumberCode"),
          icon: "error",
        });
      }
      const resp = await changeUserPhone({
        userPhone: this.phoneNumber,
        smsCode: this.phoneNumberCode,
        internationalAreaCode: this.AreaPhone.replace("/+/g", ""),
      });
      if (!resp.success) return;
      uni.switchTab({
        url: "/pages/user/User",
      });
    },
    getCodeHandle() {
      if (!this.phoneNumber) {
        uni.showToast({
          title: this.$t("user.phoneNumber"),
          icon: "error",
        });
        return;
      }
      this.codeShow = true;
      sendSms(this.phoneNumber, this.AreaPhone);
    },
    onChange(e) {
      this.timeData = e;
    },
    onFinish() {
      this.codeShow = false;
    },
    openAreaCodePopup() {
      this.showAreaCode = true;
    },
    closePopup() {
      this.searchValue = "";
      this.showAreaCode = false;
    },
    selectAreaPhone(value) {
      this.AreaPhone = value.code;
      this.searchValue = "";
      this.showAreaCode = false;
    },
    onLocation(value) {
      this.scrollIntoView = "s-" + value.en.slice(0, 1);
      this.currentView = value.en.slice(0, 1);
      console.log(value);
    },
  },
};
</script>

<style lang="scss" scoped>
.change-email {
  padding: 0upx 30upx;

  .label {
    padding: 0upx 11upx;
    margin-top: 66upx;
    font-size: 26upx;
    font-weight: 400;
    color: #878787;
  }

  .content {
    padding: 0upx 30upx;
    background: #ffffff;
    border-radius: 20upx;
    margin-top: 50upx;
  }

  .input-box {
    display: flex;

    .prefix {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 160rpx;
      margin: 26upx 0upx;
      // background: pink;
      border-right: 2rpx solid #f6f6f6;
      font-size: 30rpx;
      font-weight: 600;

      .triangle {
        width: 0;
        height: 0;
        border-top: 12rpx solid #000000;
        border-right: 10rpx solid transparent;
        border-left: 10rpx solid transparent;
        border-radius: 4rpx 4rpx 0 0;
        margin-right: 20rpx;
      }
    }

    .email-view {
      padding: 44upx 0upx 44upx 27upx;
    }
  }

  .code-views {
    border-top: 2upx solid #f6f6f6;
    padding: 44upx 0upx;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .code {
    font-size: 30upx;
    font-weight: 400;
    color: #61bdb2;
  }

  .count-down {
    font-size: 30rpx;
    font-weight: 400;
    color: #61bdb2;
  }

  .Change {
    width: 100%;
    height: 90upx;
    background: #61bdb2;
    border-radius: 20upx;
    font-size: 30upx;
    font-weight: 500;
    color: #ffffff;
    margin-top: 80upx;
    text-align: center;
    line-height: 90upx;
    &::after {
      border: none;
    }
  }
}

.area-popup {
  position: relative;
  height: 80vh;
  background: #f6f6f6;
  border-radius: 50rpx 50rpx 0px 0px;
  padding: 42rpx 0;

  &__header {
    font-size: 34rpx;
    font-family: Source Han Sans CN;
    font-weight: 500;
    text-align: center;
    color: #000000;
    margin-bottom: 40rpx;
  }

  &__input {
    display: flex;
    align-items: center;
    padding: 0 30rpx;
    margin-bottom: 10rpx;

    /deep/ .u-input--circle {
      border-radius: 100px;
      background: #fff;
      border: 0;
      padding: 26rpx 30rpx;
    }

    &__btn {
      font-size: 26rpx;
      color: #4eafa3;
      margin-left: 30rpx;
    }
  }

  &__container {
    position: relative;
    .scroll-Y {
      height: calc(90vh - 128rpx);

      .scroll-view-letter {
        height: 66rpx;
        line-height: 66rpx;
        padding: 0 30rpx;
        background-color: #f6f6f6;
        color: #878787;
      }

      .scroll-view-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 110rpx;
        padding: 0 76rpx 0 30rpx;
        background-color: #ffffff;
        font-size: 30rpx;
        color: #000000;
      }
    }
  }

  .suspension-frame {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 20upx;
    width: 40upx;
    background: rgba($color: #000000, $alpha: 0.4);
    border-radius: 20upx;
    box-sizing: border-box;
    padding: 12upx 0;
    text-align: center;

    .text {
      font-size: 22upx;
      font-weight: 400;
      color: #ffffff;
      margin: 6upx 0;

      &.active {
        margin: 6rpx auto;
        width: 34upx;
        // height: 34upx;
        background: #61bdb2;
        border-radius: 17upx;
      }
    }
  }
}

/deep/ .u-popup__content {
  background: #f6f6f6;
  border-radius: 50rpx 50rpx 0px 0px;
}

/deep/ .uni-input-placeholder {
  color: #a6a6a6;
  font-size: 30upx;
  font-weight: 400;
}
</style>
